﻿
var format1 = function (elCell, oRecord, oColumn, sData) {
    if (action == "insert" || flag == "2") {
        var str1 = [];
        var id = "id" + Math.random();
        str1.push("<option value='00'> </option>");
        str1.push("<select id='" + id + "' onchange='ChangeEvent1(this);' name='Code' style='width:95%;'>");
        str1.push("<option value='00'> </option>");
        for (var i = 0; i < CrmProduct.length; i++) {
            if (CrmProduct[i].Code == sData) {
                str1.push("<option value='" + CrmProduct[i].Caption + "' selected>" + CrmProduct[i].Caption + "</option>");
            } else {
                str1.push("<option value='" + CrmProduct[i].Caption + "'>" + CrmProduct[i].Caption + "</option>");
            }
        }
        str1.push("</select>");
        elCell.innerHTML = str1.join("");
    }
}
var format2 = function (elCell, oRecord, oColumn, sData) {
    if (sData ="") {
        sData = "";
    }
    elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Caption' style='width:95%; background: #e7eaf4;'>";
}
    var format3 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Caption' style='width:95%; background: #e7eaf4;'>";
    }
    var format4 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Money' style='width:95%; background: #e7eaf4;'>";
    }
    var format5 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Caption' style='width:95%; background: #e7eaf4;'>";
    }
    var format6 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Caption' style='width:95%; background: #e7eaf4;'>";
    }
    var format7 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\" disabled='disabled' name='Caption' style='width:95%; background: #e7eaf4;'>";
    }
    var format8 = function (elCell, oRecord, oColumn, sData) {
        var str1 = [];
        var id = "id" + Math.random();
        str1.push("<option value='00'> </option>");
        // str1.push("<select id='" + id + "' onchange='ChangeEvent1(this);' name='IsDecision' style='width:70px;'>");
        str1.push("<select id='" + id + "'  name='IsDecision' style='width:70px;'>");
        str1.push("<option value='0'>否</option>");
        str1.push("<option value='1'>是</option>");        
        str1.push("</select>");
        elCell.innerHTML = str1.join("");
    }
    var format9 = function (elCell, oRecord, oColumn, sData) {
        elCell.innerHTML = "<input type=\"text\" value=\"" + sData + "\"  name='Caption' style='width:95%;'>";
    }
    YAHOO.util.Event.addListener(window, "load", function () {
        BindData();
        hideCol();
    });
    var BindData = function () {
        YAHOO.example.RowDataMod = function () {
            var myColumnDefs1 = [
            { key: "ID", label: "ID", resizeable: true },
            { key: "Caption", label: "产品名称", resizeable: true, formatter: format1 },
            { key: "Code", label: "产品编码", resizeable: true, formatter: format3 },
            { key: "PhraseText", label: "产品类型", resizeable: true, formatter: format2 },
            { key: "Amount", label: "数量", resizeable: true, formatter: format4 },
            { key: "UnitType", label: "计量单位", resizeable: true, formatter: format5 },
            { key: "AccountReceivable", label: "应收金额", resizeable: true, formatter: format6 },
            { key: "ResignMoney", label: "已收金额", resizeable: true, formatter: format7 },
            { key: "IsSingleTrain", label: "独立内训", resizeable: true, formatter: format8 },
            { key: "TrainSum", label: "内训天数", resizeable: true, formatter: format9 }
            ];
            myDataSource1 = new YAHOO.util.DataSource([{ ID: "", Code: "", Caption: "",
                Amount: "", UnitType: "", AccountReceivable: "", ResignMoney: "", IsSingleTrain: "", TrainSum: ""
            }]);
            myDataSource1.responseType = YAHOO.util.DataSource.TYPE_JSARRAY;
            myDataSource1.responseSchema = {
                fields: ["ID", "Code", "PhraseText", "Caption", "Amount",
                      "UnitType", "AccountReceivable", "ResignMoney", "IsSingleTrain", "TrainSum"]
            };
            var myDataTable1 = new YAHOO.widget.DataTable("con_1",
            myColumnDefs1, myDataSource1, { selectionMode: "single" });
            mDataTable1 = myDataTable1;
            var i1 = 1,
            bReverseSorted1 = false;
            var trackReverseSorts1 = function (oArg) {
                bReverseSorted1 = (oArg.dir === YAHOO.widget.DataTable.CLASS_DESC);
            };
            myDataTable1.subscribe("rowMouseoverEvent", myDataTable1.onEventHighlightRow);
            myDataTable1.subscribe("rowMouseoutEvent", myDataTable1.onEventUnhighlightRow);
            myDataTable1.subscribe("rowClickEvent", myDataTable1.onEventSelectRow);
            myDataTable1.subscribe("checkboxClickEvent", function (oArgs) {
                var elCheckbox = oArgs.target;
                var oRecord = this.getRecord(elCheckbox);
                oRecord.setData("iSun_Selected", elCheckbox.checked);
            });
            GetFormValue();         
            return {
                ds: myDataSource1,
                dt: myDataTable1
            };
        } ();
    }
function hideCol() {
    mDataTable1.hideColumn(parseInt(0)); mDataTable1.refreshView();
}
var GetFormValue = function () {
    for (var i = 0; i < CrmProduct.length; i++) {
        SetFormValue1(CrmProduct);
    }
}
var SetFormValue1 = function (obj) {
    var arr = eval(obj);
    myDataSource1.liveData = arr;
    mDataTable1.getDataSource().sendRequest(null, { success: mDataTable1.onDataReturnInitializeTable }, mDataTable1);
}


var ChangeEvent1 = function (oArgs) {
    var f="";
}
//根据订单产品ID获取订单信息
var LoadCustomerData = function () {
    var vCId = "OrderProductID='" + $("#OrderProductID").val() + "'";
    YAHOO.util.Connect.asyncRequest('GET', "../../Extend/AjaxData/JSONList.aspx?r=" + Math.random() + "&type=1&entity=V_CrmCustomerInfoOrder&customCondition=" + vCId + "", //条件 
        {success: function (res) {
            try {
                var str = res.responseText;
                var arr = []; var entity = {};
                var obj = eval(str);
                $('#CustomerCode').val(obj[0].CustomerCode);
                $('#CustomerName').val(obj[0].CustomerName);
                $('#OrderTypeName').val(obj[0].OrderTypeName);
                $('#CompanyProduct').val(obj[0].CompanyProduct);
                $('#TypeName').val(obj[0].TypeName);
                $('#AddUserName').val(obj[0].AddUserName);
                $('#SaleaddName').val(obj[0].SaleadviserName);
                $('#CustomerMgrName').val(obj[0].CustomerMgrName);
                $('#LectorName').val(obj[0].LectorName);
                for (var i = 0; i < obj.length; i++) {
                    entity.ID = obj[i].ID;
                    entity.Caption = obj[i].Caption;
                    entity.ProductCode = obj[i].ProductCode;
                    entity.ProductTypeName = obj[i].ProductTypeName;
                    entity.UnitName = obj[i].UnitName;
                    entity.Amount = obj[i].Amount;
                    entity.AccountReceivable = obj[i].AccountReceivable;
                    entity.ProductResignMoney = obj[i].ProductResignMoney;
                    arr.push(entity);
                }
                CrmProduct = arr;
                SetFormValue1(CrmProduct);
            }
            catch (e) {
            }
            finally {
            }
        }, failure: function () { }
    });
}